//
//  serverViewController.m
//  tcpoctest
//
//  Created by gump on 10/22/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "serverViewController.h"
#import "WFLabel_blackShadow.h"
#import "ViewManager.h"
#import "OnlinePeopleView.h"
#import "OnlinePeopleDataSource.h"
#import "UIDevice_Jailbroken.h"
#import "taskDataSource.h"
#import "FirendCircleDataSource.h"

@interface serverViewController ()
{
    UITableView *tablev;
}

@end

@implementation serverViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"服务";
    
    tablev =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                                     style:UITableViewStyleGrouped];
    tablev.backgroundColor = CELL_BG_COLOR;
    tablev.separatorStyle=UITableViewCellSeparatorStyleNone;
    tablev.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgColor.png"]];
    tablev.backgroundView = nil;
    tablev.delegate =self;
    tablev.dataSource =self;
    [self.view addSubview:tablev];
    
    fircirHeadView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 2, 40, 40)];
}

-(void)refreshView
{
    [tablev reloadData];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if(section == 1){
        return 3;
    }else if(section == 2)
    {
        return 3;
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
        UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
        [cell setSelectedBackgroundView:selectImageView];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleBlue;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"附近的人";
        }
       
    }
    else if(indexPath.section == 1)
    {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"朋友圈";
            
            if ([DataControl shareControl].fircirDS.bShowBadge) {
                NSString *imageurl = [[DataControl shareControl] SearchHeadImage:
                                      [DataControl shareControl].fircirDS.newmail];
                if (![imageurl isEqualToString:@""]) {
                    fircirHeadView.image = [UIImage imageWithContentsOfFile:imageurl];
                    fircirHeadView.layer.cornerRadius = 5.0f;
                    fircirHeadView.layer.masksToBounds = YES;
                    
                    [cell addSubview:fircirHeadView];
                }
            }
            else
            {
                [fircirHeadView removeFromSuperview];
            }
            
        }
        else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"排行榜";
        }
        else if(indexPath.row == 2)
        {
            cell.textLabel.text = @"一见钟情";
        }
    }
    else if(indexPath.section == 2)
    {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"分享";
        }
        else if (indexPath.row == 1)
{
    cell.textLabel.text = @"充值";
}
//        else if (indexPath.row == 2)
//        {
//            
//            if ([[UIDevice currentDevice] isJailbroken]) {
//                cell.textLabel.text = @"商城";
//            }
//            else
//            {
//                cell.textLabel.text = @"帮助";
//            }
//        }
else if(indexPath.row == 2)
{
    cell.textLabel.text = @"任务";
}
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch ([indexPath section])
    {
        case 0:
        {
            switch ([indexPath row])
            {
                case 0:
                {
                    [[ViewManager sharedViewManager] showView:VI_NEARPEOPLE
                                             withIsRemoveCurV:true 
                                                withDirection:VD_RIGHT];
                    
                    bool bvisit = false;
                    switch (getNearPeopleFilterType()) {
                        case NTF_ALL:
                        {
                            bvisit = [[DataControl shareControl] IsNeedVisitNearPeople];
                        }
                            break;
                        case NTF_FEMALE:
                        {
                            bvisit = [[DataControl shareControl] IsNeedVisitNPFamale];
                        }
                            break;
                            case NTF_MALE:
                        {
                            bvisit = [[DataControl shareControl] IsNeedVisitNPMale];
                        }
                            break;
                        default:
                            break;
                    }
                    
                    if(bvisit)
                    {
                    
                    [[LogicControl sharedLogicControl] changeState:GS_WITENEAR];
                        
                        [[DataControl shareControl].LCDS addLogicCommand:LC_NEARPEOPLE];
                    }
                    
                    [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_NEARPEOPLE
                     withIsDirectShow:true];
                }
                break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch ([indexPath row])
            {
                case 0:
                {
                    //碰碰圈
                    [[ViewManager sharedViewManager] showView:VI_FIRENDCIRCLE
                                             withIsRemoveCurV:true
                                                withDirection:VD_RIGHT];

                }
                    break;
                    case 1:
                {
                    //排行榜
                    [[ViewManager sharedViewManager] showView:VI_RANKLIST
                                             withIsRemoveCurV:true
                                                withDirection:VD_RIGHT];
                }
                    break;
                    case 2:
                {
                    //一见钟情
                    [[ViewManager sharedViewManager] showView:VI_LOVE
                                             withIsRemoveCurV:true
                                                withDirection:VD_RIGHT];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            switch ([indexPath row])
            {
                case 0:
                {
                    //友盟
                    [UMSocialSnsService presentSnsController:self
                                                         appKey:T_UMENG
                                                      shareText:[NSString stringWithFormat:@"%@,%@",T_PROJECTNAME,T_SHARETO]
                                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                                shareToSnsNames:[NSArray arrayWithObjects:
                                                                 UMShareToSina,
                                                                 UMShareToTencent,
                                                                 UMShareToRenren,
                                                                 UMShareToDouban,
                                                                 nil]
                                                       delegate:self];
                    
                }
                    break;
                    case 1:
                {
                    
                    if ([[UIDevice currentDevice] isJailbroken]) {
                        [[ViewManager sharedViewManager] showView:VI_RECHARGE
                                                 withIsRemoveCurV:false
                                                    withDirection:VD_RIGHT];

                    }
                     else
                     {
                         [[ViewManager sharedViewManager] showView:VI_PURCHASE
                                                  withIsRemoveCurV:false
                                                     withDirection:VD_RIGHT];
                     }
                    
                    
                }
                    break;
//                case 2:
//                {
//                    if ([[UIDevice currentDevice] isJailbroken]) {
//                        [[ViewManager sharedViewManager] showView:VI_SHOP
//                                                 withIsRemoveCurV:false
//                                                    withDirection:VD_RIGHT];
//                        
//                    }
//                    else
//                    {
//                        [[ViewManager sharedViewManager] showView:VI_HELP_WEB
//                                                 withIsRemoveCurV:false
//                                                    withDirection:VD_RIGHT];
//                    }
//                }
//                    break;
                case 2:
                {
                    [[ViewManager sharedViewManager] showView:VI_TASK
                                             withIsRemoveCurV:false
                                                withDirection:VD_RIGHT];
                }
                    break;
                default:
                    break;
            }
        }
            break;

        default:
            break;
    }
}

#pragma mark -umeng deleget-
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`response`的`viewControllerType`来得到页面类型
    if (response.viewControllerType == UMSViewControllerShareEdit) {
        //根据`responseCode`得到发送结果,如果分享成功
        if(response.responseCode == UMSResponseCodeSuccess)
        {
            //得到分享到的微博平台名
            
            NSString *sharename = [[response.data allKeys] objectAtIndex:0];
            if ([sharename isEqualToString:@"sina"]) {
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_SINA
                 withIsDirectShow:true];
            }
            else if([sharename isEqualToString:@"tencent"])
            {
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_QQ
                 withIsDirectShow:true];
            }
            else if([sharename isEqualToString:@"renren"])
            {
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_RENREN
                 withIsDirectShow:true];
            }
            else if([sharename isEqualToString:@"qzone"])
            {
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_QQ
                 withIsDirectShow:true];
            }
            else if([sharename isEqualToString:@"douban"])
            {
                [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_DOUBAN
                 withIsDirectShow:true];
            }
        }
    }
}
@end
